package zy.blogs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import zy.blogs.entity.ParkingLot;
import zy.blogs.entity.PricingStandard;
import zy.blogs.entity.SysUser;
import zy.blogs.service.ParkingLotService;
import zy.blogs.service.SysUserService;

import java.util.List;
@Controller
@RequestMapping("/parking")
public class ParkingLotController {
    ParkingLotService service;
    ParkingLotService pricingStandardService;
    @Autowired
    public ParkingLotController(ParkingLotService service
            ,ParkingLotService pricingStandardService){
        this.service=service;
        this.pricingStandardService=pricingStandardService;
    }
    @RequestMapping("/list")
    public String list(Model model){
        List<ParkingLot> entities=service.getAll();
        model.addAttribute("list",entities);
        return "/parking/list";
    }
    @RequestMapping(value = "/insert")
    public String insert(Model model){
        model.addAttribute("pricing",new ParkingLot());
        List<ParkingLot> pricingList=pricingStandardService.getAll();
        model.addAttribute("pricingList",pricingList);
        return "/parking/edit";
    }

    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable("id")Long id,Model model){
       ParkingLot entity=service.getById(id);
       model.addAttribute("parking",entity);
       List<ParkingLot> pricingList = pricingStandardService.getAll();
       model.addAttribute("pricingList",pricingList);
       return "/parking/edit()";
    }

    @RequestMapping(value = "/save",method= RequestMethod.POST)
    public String save(@ModelAttribute("pricing")ParkingLot entity, @RequestParam("pricingId") Long pricingId){
        PricingStandard pricing= pricingStandardService.getById(pricingId).getPricingStandard();
        entity.setPricingStandard(pricing);
        boolean result=entity.getId()!=null?service.update(entity):service.insert(entity);
        return "redirect:/parking/list()";
    }
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id")Long id,Model model){
        ParkingLot entity=service.getById(id);
        model.addAttribute("parking",entity);
        return "/parking/detail";
    }
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id")Long id){
        boolean result=service.delete(id);
        return "redirect:/parking/detail";
    }

}
